﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Comm;
using VK.Casemeet.Merchants;

namespace VK.Casemeet.Web.Admin.Merchants
{
    public partial class ShopEdit : System.Web.UI.Page
    {
        public string map = CS.ShopMap;
        public int shopID = 0, area1, area2, area3;
        public string imgUrl = "Null.jpg", logoUrl = "Null.jpg";

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="店铺",Url="/Admin/Merchants/ShopList.aspx"},
                new MapPath{ Text="编辑店铺",Url="#"}
            };

            shopID = Util.GetQueryInt("shopID");

            if (!IsPostBack)
            {
                ShopHelper.BindTypeDDL(ddlType, OptionType.Default);
                ShopHelper.BindStateDDL(ddlState, OptionType.Select);
                ShopHelper.BindLevelRAD(radLevel);

                LoadData();
            }
        }

        internal void LoadData()
        {
            if (shopID != 0)
            {
                ShopEntity shop = ShopBLL.GetShop(shopID);
                ddlType.SelectedValue = shop.Type.ToString();
                txtName.Text = shop.Name;
                radLevel.SelectedValue = shop.Level.ToString();
                txtTelephone.Text = shop.Telephone;
                txtCapital.Text = shop.Capital == 0 ? string.Empty : shop.Capital.ToString();
                area1 = shop.ProvinceID;
                area2 = shop.CityID;
                area3 = shop.AreaID;
                txtAddress.Text = shop.Address;
                txtTelephone.Text = shop.Telephone;
                map = shop.Map;
                txtHits.Text = shop.Hits.ToString();
                chkIsCertification.Checked = shop.IsCertification;
                ddlState.SelectedValue = shop.State.ToString();
                txtNotice.Text = shop.Notice;
                imgUrl = shop.LicenceUrl;
                logoUrl = shop.LogoUrl;
                txtContact.Text = shop.Contact;
                txtEmail.Text = shop.Email;
                txtCompany.Text = shop.Company;
                txtQQ.Text = shop.QQ;
                txtSiteUrl.Text = shop.SiteUrl;
                txtAccount.Text = shop.Account;
                txtPassword.Text = DES.Decrypt(shop.Password, CS.ShopAdminPassKey);
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            ShopEntity shop = LoadEntity();
            shop.ShopID = shopID;

            if (ShopBLL.UpdateShop(shop) > 0)
            {
                Message.Msg(this.Page, CS.MsgEditSuccess, MessageType.Success);
            }
            else
            {
                Message.Msg(this.Page, CS.MsgEditFailure, MessageType.Failure);
            }
            LoadData();
        }

        protected ShopEntity LoadEntity()
        {
            ShopEntity shop = ShopBLL.GetShop(shopID);
            shop.Type = int.Parse(ddlType.SelectedValue);
            shop.Name = txtName.Text.Trim();
            shop.Level = int.Parse(radLevel.SelectedValue);
            shop.Telephone = txtTelephone.Text.Trim();
            shop.Capital = string.IsNullOrEmpty(txtCapital.Text.Trim()) ? 0 : int.Parse(txtCapital.Text.Trim());
            shop.ProvinceID = Util.GetFormInt("ddlArea1");
            shop.CityID = Util.GetFormInt("ddlArea2");
            shop.AreaID = Util.GetFormInt("ddlArea3");
            shop.Address = txtAddress.Text.Trim();
            shop.Map = Util.GetFormString("txtMap");
            shop.Hits = int.Parse(txtHits.Text.Trim());
            shop.IsCertification = chkIsCertification.Checked;
            shop.State = int.Parse(ddlState.SelectedValue);
            shop.Notice = txtNotice.Text.Trim();
            shop.LicenceUrl = Util.GetFormString("hidImgUrl");
            shop.Email = txtEmail.Text.Trim();
            shop.Company = txtCompany.Text.Trim();
            shop.Contact = txtContact.Text.Trim();
            shop.Account = txtAccount.Text.Trim();
            shop.Password = DES.Encrypt(txtPassword.Text, CS.ShopAdminPassKey);
            shop.LogoUrl = Util.GetFormString("hidLogoUrl");
            shop.QQ = txtQQ.Text.Trim();
            shop.SiteUrl = txtSiteUrl.Text.Trim();

            return shop;
        }
    }
}